package com.hibase.core.excel.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hibase.common.exception.HibaseAssert;
import com.hibase.core.excel.entity.MdsExcelBizDataDetail;
import com.hibase.core.excel.mapper.MdsExcelBizDataDetailMapper;
import com.hibase.core.excel.service.MdsExcelBizDataDetailService;
import com.hibase.core.excel.util.Validate;
import com.hibase.core.excel.vo.bizdata.detail.MdsExcelBizDataDetailByImportTemplateSearchVO;
import com.hibase.core.excel.vo.bizdata.detail.MdsExcelBizDataDetailSearchVO;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Excel 模板配置详情表 Service 实现类
 *
 * @author <a href="mailto:xiongliyi@hichain.com">Ian Xiong</a>
 * @since 1.4.35-V2
 */
@Service
public class MdsExcelBizDataDetailServiceImpl extends BaseServiceImpl<MdsExcelBizDataDetailMapper, MdsExcelBizDataDetail> implements MdsExcelBizDataDetailService {

    @Override
    public PageInfo queryPage(MdsExcelBizDataDetailSearchVO searchVO) {
        QueryWrapper<MdsExcelBizDataDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MdsExcelBizDataDetail::getBeDeleted, Boolean.FALSE);

        if (StrUtil.isNotEmpty(searchVO.getKeyWord())) {
            queryWrapper.lambda().like(MdsExcelBizDataDetail::getFieldName, searchVO.getKeyWord());
        }
        if (StrUtil.isNotEmpty(searchVO.getFieldName())) {
            queryWrapper.lambda().like(MdsExcelBizDataDetail::getFieldName, searchVO.getFieldName());
        }
        if (StrUtil.isNotEmpty(searchVO.getBizDataId())) {
            queryWrapper.lambda().eq(MdsExcelBizDataDetail::getBizDataId, searchVO.getBizDataId());
        }
        if (StrUtil.isNotEmpty(searchVO.getFieldKey())) {
            queryWrapper.lambda().like(MdsExcelBizDataDetail::getFieldKey, searchVO.getFieldKey());
        }
        if (StrUtil.isNotEmpty(searchVO.getFieldType())) {
            queryWrapper.lambda().like(MdsExcelBizDataDetail::getFieldType, searchVO.getFieldType());
        }

        queryWrapper.lambda().orderByAsc(MdsExcelBizDataDetail::getFieldSort);
        return super.page(searchVO, queryWrapper);
    }

    /**
     * 通过模板配置Id查询
     *
     * @param searchVO 查询条件
     * @return 返回数据详情分页列表
     */
    @Override
    public PageInfo queryPageByTemplateId(MdsExcelBizDataDetailByImportTemplateSearchVO searchVO) {
        Page<String> page = PageHelper.startPage(searchVO.getCurrentPage(), searchVO.getPageSize());
        baseMapper.queryPageByImportId(searchVO.getImportTemplateId(), this.quickLookQuery(searchVO, new QueryWrapper<>()));
        return new PageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBizDataDetail(MdsExcelBizDataDetail entity) {
        hasFieldKey(entity.getBizDataId(), entity.getFieldKey(), Boolean.FALSE);
        return baseMapper.insert(entity) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizDataDetail(MdsExcelBizDataDetail entity) {
        hasFieldKey(entity.getBizDataId(), entity.getFieldKey(), Boolean.TRUE);
        return baseMapper.updateById(entity);
    }

    @Override
    public List<MdsExcelBizDataDetail> selectInIds(Set<String> ids) {
        return baseMapper.selectList(Wrappers.<MdsExcelBizDataDetail>lambdaQuery().in(MdsExcelBizDataDetail::getId, ids));
    }

    @Override
    public Map<String, MdsExcelBizDataDetail> findInIdsToMap(Set<String> ids) {
        List<MdsExcelBizDataDetail> mdsExcelBizDataDetails = selectInIds(ids);
        HibaseAssert.notNull(mdsExcelBizDataDetails, "Mds Excel BizData Details cannot be empty");
        return mdsExcelBizDataDetails.stream().collect(Collectors.toMap(item -> item.getId(), item -> item));
    }


    /**
     * @param bizDataId
     * @param fieldKey
     * @return
     */
    private void hasFieldKey(String bizDataId, String fieldKey, Boolean isUpdate) {
        QueryWrapper<MdsExcelBizDataDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MdsExcelBizDataDetail::getFieldKey, fieldKey);

        if (isUpdate) {
            queryWrapper.lambda().ne(MdsExcelBizDataDetail::getBizDataId, bizDataId);
        } else {
            queryWrapper.lambda().eq(MdsExcelBizDataDetail::getBizDataId, bizDataId);
        }

        Integer count = baseMapper.selectCount(queryWrapper);

        Validate.isTrue(count == 0, fieldKey + " already exists");
    }
}
